package com.delicious.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.delicious.dao.impl.NewsDaoImpl;
import com.delicious.entity.News;

public class NewsServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageSize = 7;
		int pageNum = 0;
		
		if(request.getParameter("pageNum")!=null && !request.getParameter("pageNum").isEmpty())
			pageNum =Integer.parseInt(request.getParameter("pageNum"));
		else
			pageNum = 1;
		int rowNo = (pageNum-1)*pageSize;
		List<News> list = new NewsDaoImpl().getNewsPaging(pageNum, pageSize);
		int rows = new NewsDaoImpl().getNewsPaging(0, 0).size();
		int totalPage =(int) Math.ceil((double)rows/pageSize);
		request.setAttribute("list", list);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("rowNo", rowNo);
		request.getRequestDispatcher("news.jsp").forward(request, response);
	}

}
